package com.vpx.server.framework.authority;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lihui
 * @title: vpx
 * @module TODO
 * @description: session管理器
 * @date 2021/9/17 16:38
 */
@Component
public class SessionStoreManager {
    private static SessionStoreManager sessionStoreManager;

    private static Map<String, SessionInfo> sessionStore = new ConcurrentHashMap<String, SessionInfo>();

    @PostConstruct
    public void init() {
        sessionStoreManager = this;
    }

    public static void setSession(String key, SessionInfo sessionInfo){
        if(sessionInfo == null){
            remove(key);
            return;
        }
        SessionInfo session = new SessionInfo();
        session.from(sessionInfo);
        sessionStore.put(key, session);
    }

    public static SessionInfo getSession(String key){
        SessionInfo session =  sessionStore.get(key);
        if(session == null){
            return null;
        }
        SessionInfo result = new SessionInfo();
        result.from(session);
        return result;
    }

    public static void remove(String key){
        sessionStore.remove(key);
    }
}
